package org.his.controller.modular.yisheng;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.his.dao.CaseInfoDao;
import org.his.dao.InstrInfoDao;
import org.his.dao.ReserveDao;
import org.his.dao.ReserveInstrDao;
import org.his.pojo.binglidangan.CaseInfo;
import org.his.pojo.liucheng.Reserve;
import org.his.pojo.liucheng.ReserveInstr;
import org.his.pojo.quanxuan.User;
import org.his.pojo.zhiliaoyi.InstrInfo;
import org.his.utils.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/bingliluru")
@Controller
public class BingLiLuRuController {

    @Autowired
    private ReserveDao reserveDao;
    @Autowired
    private ReserveInstrDao reserveInstrDao;

    @Autowired
    private InstrInfoDao instrInfoDao;

    @Autowired
    private CaseInfoDao caseInfoDao;

    @RequestMapping("/index")
    public String index()throws Exception{
        return "modular/yisheng/index";
    }


    //预约仪器页面
    @RequestMapping("/yuyuezhiliaoyi")
    public String yuyuezhiliaoyi() throws Exception{
        return "modular/yisheng/yuyuezhiliaoyi";
    }

    //开药
    @RequestMapping("/kaiyao")
    public String kaiyao() throws Exception{
        return "modular/yisheng/kaiyao";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer limit, HttpSession session)throws Exception{
        Map<String, Object> map = new HashMap<>();

        Page<Reserve> pager = PageHelper.startPage(page,limit);

        User user = (User) session.getAttribute(Common.LOGIN_USER);

        map.put("data", reserveDao.getbingliluru(user.getUserid()));
        map.put("count", pager.getTotal());
        map.put("code", 0);
        map.put("msg", null);


        return map;
    }


    @RequestMapping("/zhiliaoyiList")
    @ResponseBody
    public List<InstrInfo> zhiliaoyiList() throws Exception{
        return instrInfoDao.getList(null);
    }

    @RequestMapping("/zhiliaoyijiage")
    @ResponseBody
    public Integer zhiliaoyijiage(Integer instrId) throws Exception{
        return instrInfoDao.getjineById(instrId);
    }

    @RequestMapping("/getjiuzhen")
    @ResponseBody
    public List<Reserve> getjiuzhen()throws Exception{
        return reserveDao.getjiuzhenid();
    }

    @RequestMapping("/getCaseInfoById")
    @ResponseBody
    public CaseInfo getCaseInfoById(String memberCaseNo) throws Exception{
        return caseInfoDao.getCaseInfo(memberCaseNo);
    }


    @RequestMapping("/editCaseInfo")
    @ResponseBody
    public Map<String,Object> editCaseInfo(CaseInfo caseInfo)throws Exception{
        Map<String,Object> map = new HashMap<>();

        caseInfoDao.update(caseInfo);
        map.put("medicalRecordNo",caseInfo.getMemberCaseNo().getMedicalRecordNo());

        return map;
    }


    @RequestMapping("/editType")
    @ResponseBody
    public Map<String,Object> editType (String medicalRecordNo) throws  Exception{
        Map<String,Object> map = new HashMap<>();

        reserveDao.updType(medicalRecordNo);

        return map;
    }

    @RequestMapping("/yaowutable")
    public  String yaowutable()throws Exception{
        return "modular/yisheng/yaowuTable";
    }

    @RequestMapping("/lishi")
    public  String lishi()throws Exception{
        return "modular/yisheng/lishixinxichaxun";
    }

    @RequestMapping("/filesUpload")
    @ResponseBody
    public Map<String,Object> filesUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        String pathRoot = request.getServletContext().getRealPath("/assets/images/zhiliaoimg");
        Map<String,Object> map = new HashMap<>();
        try {
            if(!file.isEmpty()){
                String filename = System.currentTimeMillis()+"_"+file.getOriginalFilename();//时间戳加源文件名
                File dest = new File(pathRoot + File.separator + filename);
                file.transferTo(dest);
                map.put("msg","ok");
                map.put("imgPath","assets/images/zhiliaoimg/"+filename);
            }
        } catch (IOException e) {
            e.printStackTrace();
            map.put("msg","error");
        } catch (IllegalStateException e) {
            e.printStackTrace();
            map.put("msg","error");
        }
        return map;
    }



}
